package com.anolesoft.amljmes.screen.service.Impl;

import com.anolesoft.amljmes.screen.dto.MesOrderAssess;
import com.anolesoft.amljmes.screen.dto.MesOrderTimeoutLogs;
import com.anolesoft.amljmes.screen.mapper.MesOrderTimeoutLogsMapper;
import com.anolesoft.amljmes.screen.service.MesOrderTimeoutLogsService;
import com.anolesoft.amljmes.screen.vo.MesOrderViewVo;
import com.anolesoft.common.core.domain.LoginUser;
import com.anolesoft.yr.demo.PageResult;
import com.anolesoft.yr.exception.ServiceException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class MesOrderTimeoutLogsServiceImpl implements MesOrderTimeoutLogsService {

    @Resource
    private MesOrderTimeoutLogsMapper mapper;

    @Override
    public List<MesOrderViewVo> querytongji(String jx) {
        List<MesOrderTimeoutLogs> list = new ArrayList<>();
        if (jx != null && jx != "") {
            list = mapper.querytongji(jx.toUpperCase());
        } else {
            list = mapper.querytongjiwu();
        }
        Set<String> set = new TreeSet<>();
//        List<MesOrderTimeoutLogs> list1 = list.stream().distinct().collect(Collectors.toList());
        int i = set.size();
        for (MesOrderTimeoutLogs item:list) {
            set.add(item.getFactoryId());
        }
        List<MesOrderViewVo> listdata = new ArrayList<>();
        int a = 0; // 在制
        int b = 0; // 待归档
        int c = 0; // 1-10
        int d = 0; // 11-20
        int e = 0; // 21-30
        int f = 0; // 30+
        int g = 0;
        Iterator<String> iterator = set.iterator();
        while (iterator.hasNext()){
            String cdh = iterator.next();
            List<MesOrderTimeoutLogs> aa = list.stream().filter(item -> cdh.equals(item.getFactoryId())).collect(Collectors.toList());
            for (MesOrderTimeoutLogs item:aa) {
                if (item.getTimeoutType().equals("0")) {
                    Integer a1 = item.getGeshu();
                    if (item.getCxtime().equals("0")) {
                        Integer a00 = item.getGeshu();
                        c+=a00;
                    } else if (item.getCxtime().equals("1")) {
                        Integer a01 = item.getGeshu();
                        d+=a01;
                    } else if (item.getCxtime().equals("2")) {
                        Integer a02 = item.getGeshu();
                        e+=a02;
                    } else {
                        Integer a03 = item.getGeshu();
                        f+=a03;
                    }
                    a+=a1;
                } else {
                    Integer a2 = item.getGeshu();
                    b+=a2;
                }

            }
            g++;
            MesOrderViewVo vo = new MesOrderViewVo();
            vo.setObjuid(g + "");
            vo.setFactoryId(cdh);
            vo.setZaizhi(a + "");
            vo.setDaiguidang(b + "");
            vo.setYishi(c + "");
            vo.setShiershi(d + "");
            vo.setErshisanshi(e + "");
            vo.setSanshijia(f + "");
            listdata.add(vo);
            a = 0;
            b = 0;
            c = 0;
            d = 0;
            e = 0;
            f = 0;
        }
        return listdata;
    }

    @Override
    public PageResult querymingxi(MesOrderTimeoutLogs mesOrderTimeoutLogs) {
        if (mesOrderTimeoutLogs.getJx() != null) {
            mesOrderTimeoutLogs.setJx(mesOrderTimeoutLogs.getJx().toUpperCase());
        }
        int totalcount = mapper.countPageList(mesOrderTimeoutLogs);
        Integer pageNum = mesOrderTimeoutLogs.getPageNum();
        Integer pageSize = mesOrderTimeoutLogs.getPageSize();
        Integer pageBug = pageNum * pageSize > totalcount ? totalcount : pageNum * pageSize;
        Integer pageSmall = (pageNum - 1) * pageSize;
        mesOrderTimeoutLogs.setPageBug(pageBug);
        mesOrderTimeoutLogs.setPageSmall(pageSmall);
        List<MesOrderAssess> allList =  mapper.selectPageList(mesOrderTimeoutLogs);
        PageResult pageResult = new PageResult(allList, pageSize, pageNum, totalcount);
        return pageResult;
    }

    @Override
    public String countkaohe(String zzjltm) {
        return mapper.countkaohe(zzjltm);
    }

    @Override
    public int insertkaohe(MesOrderAssess mesOrderAssess) {
        LoginUser loginUser = com.anolesoft.yr.util.SecurityUtils.getLoginUser();
        String uid = loginUser.getEmpUid();
        String zzjltm = mesOrderAssess.getZzjltm();
        String objuid = mapper.countkaohe(zzjltm);
        mesOrderAssess.setCreatUser(uid);
        mesOrderAssess.setUpdateUser(uid);
        mesOrderAssess.setInformantUser(uid);
        mesOrderAssess.setArtUser(uid);
        mesOrderAssess.setPdUser(uid);
        mesOrderAssess.setQualityUser(uid);
        mesOrderAssess.setAssessmentUser(uid);
        if (mesOrderAssess.getAssessment().equals("0")) {
            mesOrderAssess.setAssessState("1");
        }
        if (mesOrderAssess.getAssessment().equals("1")) {
            mesOrderAssess.setAssessState("0");
        }
        int a = 0;
        if (objuid == null) {
            a = mapper.insertkaohe(mesOrderAssess);
        } else {
            a = mapper.updatekaohe(mesOrderAssess);
        }
        return a;
    }

    @Override
    public int updatekaohe(MesOrderAssess mesOrderAssess) {
        return mapper.updatekaohe(mesOrderAssess);
    }

    @Override
    public int updatereason(MesOrderAssess mesOrderAssess) {
        LoginUser loginUser = com.anolesoft.yr.util.SecurityUtils.getLoginUser();
        String uid = loginUser.getEmpUid();
        mesOrderAssess.setUpdateUser(uid);
        mesOrderAssess.setInformantUser(uid);
        int a = mapper.updatereason(mesOrderAssess);
        return a;
    }

    @Override
    public int updateshengchan(MesOrderAssess mesOrderAssess) {
        LoginUser loginUser = com.anolesoft.yr.util.SecurityUtils.getLoginUser();
        String fac = loginUser.getFacCode();
        String pduser = mesOrderAssess.getPdUser();
        String a = mapper.countpop(pduser,fac);
        int b = 0;
        if (a == null) {
            b = 0;
//            throw new ServiceException("此胸卡号无效");
        } else {
            mesOrderAssess.setPdUser(a);
            b = mapper.updateshengchan(mesOrderAssess);
        }
        return b;
    }

    @Override
    public int updatejishu(MesOrderAssess mesOrderAssess) {
        LoginUser loginUser = com.anolesoft.yr.util.SecurityUtils.getLoginUser();
        String fac = loginUser.getFacCode();
        String artuser = mesOrderAssess.getArtUser();
        String a = mapper.countpop(artuser,fac);
        int b = 0;
        if (a == null) {
            b = 0;
//            throw new ServiceException("当前胸卡号无效");
        } else {
            mesOrderAssess.setArtUser(a);
            b = mapper.updatejishu(mesOrderAssess);
        }
        return b;
    }

    @Override
    public int updatezhiliang(MesOrderAssess mesOrderAssess) {
        LoginUser loginUser = com.anolesoft.yr.util.SecurityUtils.getLoginUser();
        String fac = loginUser.getFacCode();
        String qualityuser = mesOrderAssess.getQualityUser();
        String a = mapper.countpop(qualityuser,fac);
        int b = 0;
        if (a == null) {
            b = 0;
//            throw new ServiceException("当前胸卡号无效");
        } else {
            mesOrderAssess.setQualityUser(a);
            b = mapper.updatezhiliang(mesOrderAssess);
        }
        return b;
    }
}
